#ifndef __SCRIPT_H__
#define __SCRIPT_H__


class PacketDecoder;

typedef Int32 ScriptId;
//typedef boost::function< Int32(lua_State *) > LuaFunction;
typedef Int32 (*LuaFunction)(lua_State *);
#pragma pack(1)
class Script
{

public:
	Script(ScriptId nScriptId);
	virtual ~Script();
	void SetScriptId(ScriptId nScriptId){m_nScriptId = nScriptId;};
	ScriptId GetScriptId(){ return m_nScriptId;};

	lua_State* GetL(){return L;}
	bool IsValid(){return L != NULL;}

	bool DoFile(std::string strFileName);
	bool DoString(std::string str);
	void RegisterFunction(std::string strFunctionName , LuaFunction Func);
	bool HasFunction(std::string strFunction);
	Int32 CallFunction(std::string strFunction);
	Int32 CallFunction1(std::string strFunction,Int32 nParam1);
	Int32 CallFunction2(std::string strFunction,Int32 nParam1,Int32 nParam2);
	Int32 CallFunction2(std::string strFunction,Int32 nParam1,std::string nParam2);
	Int32 CallFunction3(std::string strFunction,Int32 nParam1,Int32 nParam2,Int32 nParam3);
	Int32 CallFunction4(std::string strFunction,Int32 nParam1,Int32 nParam2,Int32 nParam3,Int32 nParam4);
	Int32 CallFunction5(std::string strFunction,Int32 nParam1,Int32 nParam2,Int32 nParam3,Int32 nParam4,Int32 nParam5);
	Int32 CallFunction6(std::string strFunction,Int32 nParam1,Int32 nParam2,Int32 nParam3,Int32 nParam4,Int32 nParam5,Int32 nParam6);

	Int32 CallFunction1(std::string strFunction,std::string nParam1);
	std::string GetFileName(){return m_strFileName;}
	void SetFileName(std::string strFileName){m_strFileName = strFileName;}

private:
	void push(const Int32 &value){lua_pushinteger(L, value);}
	//void push(const float &value){lua_pushnumber(L, value);}
	//void push(const double &value){lua_pushnumber(L, value);}
	void push(const std::string &value){lua_pushstring(L, value.c_str());}
	void get(Int32 &value) const{value = lua_tointeger(L, -1);}
	//void get(float &value) const{value = (float)lua_tonumber(L, -1);}
	//void get(double &value) const{value = lua_tonumber(L, -1);}
	void get(std::string &value) const{value = (char*)lua_tostring(L, -1);}
private:
	ScriptId			m_nScriptId;
	std::string			m_strFileName;
	lua_State*			L;
};
#pragma pack()
#endif